package net.biancheng.controller;

import java.util.List;

import net.biancheng.bean.SysRole;
import net.biancheng.common.constant.CommonConstant;
import net.biancheng.common.core.BaseController;
import net.biancheng.common.core.CommonResult;
import net.biancheng.common.model.LoginUser;
import net.biancheng.common.utils.SecurityUtils;
import net.biancheng.service.ISysRoleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.biancheng.common.annotation.Log;
import net.biancheng.common.enums.BusinessType;
import net.biancheng.common.core.page.TableDataInfo;

/**
 * 角色信息Controller
 * 
 * @author lb
 * @date 2024-01-11
 */
@RestController
@RequestMapping("/system/role")
public class SysRoleController extends BaseController
{
    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询角色信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRole sysRole)
    {
        startPage();
        List<SysRole> list = sysRoleService.selectSysRoleList(sysRole);
        return getDataTable(list);
    }

    /**
     * 获取角色信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping(value = "/{id}")
    public CommonResult getInfo(@PathVariable Long id)
    {
        return CommonResult.success(sysRoleService.selectSysRoleById(id));
    }

    /**
     * 新增角色信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色信息", businessType = BusinessType.INSERT)
    @PostMapping
    public CommonResult add(@RequestBody SysRole sysRole)
    {
        if (CommonConstant.NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(sysRole))) {
            return CommonResult.error("新增角色'" + sysRole.getRoleName() + "'失败，角色名称已存在");
        }else if (CommonConstant.NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(sysRole))) {
            return CommonResult.error("新增角色'" + sysRole.getRoleName() + "'失败，角色标识已存在");
        }
        return toResult(sysRoleService.insertSysRole(sysRole));
    }

    /**
     * 修改角色信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public CommonResult edit(@RequestBody SysRole sysRole)
    {
        if (CommonConstant.NOT_UNIQUE.equals(sysRoleService.checkRoleNameUnique(sysRole))) {
            return CommonResult.error("修改角色'" + sysRole.getRoleName() + "'失败，角色名称已存在");
        }else if (CommonConstant.NOT_UNIQUE.equals(sysRoleService.checkRoleKeyUnique(sysRole))) {
            return CommonResult.error("修改角色'" + sysRole.getRoleName() + "'失败，角色标识已存在");
        }

        return toResult(sysRoleService.updateSysRole(sysRole));
    }

    /**
     * 删除角色信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public CommonResult remove(@PathVariable Long[] ids)
    {
        return toResult(sysRoleService.deleteSysRoleByIds(ids));
    }
}
